#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#include <immintrin.h>
//#include <emmintrin.h>
#define int long long
#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define fi first
#define se second
#define pii pair<int,int>
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define rep(i,  b) for(int i=1,i##end=b;i<=i##end;i++)
using namespace std;
const int N=1e6+5,base=999983,Mod=998244353;
//char buf[(1<<21)+5],*p1,*p2;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline void chmx(int &x,int y){(x<y)&&(x=y);}
inline void chmn(int &x,int y){(x>y)&&(x=y);}
inline void Add(int &x,int y){(x=x+y+Mod)%=Mod;}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
void print(int n){
    if(n<0){
        putchar('-');
        n*=-1;
    }
    if(n>9) print(n/10);
    putchar(n%10+'0');
}
int n;
vector<pii>q[N];
int siz[N];
int fa[N][21],f[N];
int lg[N],dep[N],bian[N];
int bg[N],ed[N],id[N],idx;
void dfs(int x,int faz){
	if(!q[x].size())
	++idx,id[idx]=x;
	bg[x]=idx;
	if(!q[x].size()) siz[x]++;
	dep[x]=dep[faz]+1;
	For(i,1,20) fa[x][i]=fa[fa[x][i-1]][i-1];
	for(auto y:q[x]){
		f[y.fi]=f[x]+y.se;
		bian[y.fi]=y.se;
		dfs(y.fi,x);
		siz[x]+=siz[y.fi];
	}ed[x]=idx;
}inline int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	Rof(i,20,0) if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
	if(x==y) return x;
	Rof(i,20,0)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
//unordered_map<int,int>dp[N];
vector<int>dp[N];
inline int dis(int x,int y){
	return f[x]+f[y]-2*f[LCA(x,y)];
}int MX=0;
bool FLAG=0;
void DP(int x,int faz){
	if(!q[x].size())return;
	int L=0,R=0;
	for(auto y:q[x]){
		DP(y.fi,x);
		if(!L) L=y.fi;
		else R=y.fi;
	}
	For(X,0,ed[L]-bg[L])For(Y,0,ed[R]-bg[R]){
		int i=id[X+bg[L]],j=id[Y+bg[R]];
		if(dp[L][X]+bian[L]+f[j]-f[x]<=MX){
			dp[x][X+bg[L]-bg[x]]=min(dp[x][X+bg[L]-bg[x]],dp[R][Y]+bian[R]);
		}
	}
	swap(L,R);
	For(X,0,ed[L]-bg[L])For(Y,0,ed[R]-bg[R]){
		int i=id[X+bg[L]],j=id[Y+bg[R]];
		if(dp[L][X]+bian[L]+f[j]-f[x]<=MX){
			dp[x][X+bg[L]-bg[x]]=min(dp[x][X+bg[L]-bg[x]],dp[R][Y]+bian[R]);
		}
	}
}
inline bool check(int x){
	MX=x;
	FLAG=0;
	For(i,1,n)For(j,0,ed[i]-bg[i])dp[i][j]=1e16;
	For(i,1,n)if(!q[i].size()){
		dp[i][0]=0;
	}
//	cout<<MX<<endl;
	DP(1,0);
//	puts("");puts("");
	int ans=1e16;
	For(i,0,ed[1]-bg[1]) ans=min(ans,dp[1][i]);
	if(ans==1e16)return 0;
	return 1;
}
signed main(){
	freopen("trip.in","r",stdin);
	freopen("trip.out","w",stdout);
	// ios::sync_with_stdio(false);
	// cin.tie(0); cout.tie(0);
	lg[0]=-1;
	For(i,1,N-5) lg[i]=lg[i/2]+1;
	n=read();
	For(i,2,n){
		fa[i][0]=read();int w=read();
		q[fa[i][0]].emplace_back(i,w);
	}
	dfs(1,0);
	For(i,1,n)dp[i].resize(ed[i]-bg[i]+1); 
	int L=0,R=1e16,res=0;
	while(L<=R){
		int mid=(L+R)>>1;
		if(check(mid))R=mid-1,res=mid;
		else L=mid+1;
	}printf("%lld\n",res);
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}

